|============================|
|Equirawn's Moray IK Tutorial|
|============================|

Version 1.0
7th April 1999

Introduction
------------
This tutorial is designed to help people to learn some of the basics on the IK feature in Moray 3.1 . First, I want to dispell a myth that I think is growing - the basic stuff I am about to teach you is NOT difficult to do. I managed to work it out in about 20 minutes.

The format of this tutorial is that I explain a step, and then provide a Moray scene file of that step. For the purposes of this tutorial we will create a simple human leg made of spheres, and then add the IK. I recommend that you work through the tutorial first, including the tutorialpartX.mdl files, and then when you are comfortable with the basics I teach you, have a look at the Person1.mdl file, which contains an extension of the leg project - a whole person made of simple shapes. All the body parts have IK added, so have fun posing it.

Also, I'm not saying that this method of setting up IK is the only way or even the correct way. I'm just saying that it worked for me, and it might work for you too. Don't take this document as the Holy Writ of Inverse Kinematics :)

So if you're ready, lets get started!


Step 1.
-------

Before we even begin to think about working with IK, we must have something to apply IK to. When you are building a model that you want to have IK applied to, I reccommend that you don't start grouping the various parts of the model. You can apply CSG to any objects that are supposed to make up one seperate object (such as a foot), but don't start grouping objects (such as "Leg", made up of Foot, Lower Leg, Knee, Upper Leg and Thigh). The reason for this is that Moray's IK is uses groups to control how objects are linked, and so specific groupings must be made depending on where joints are supposed to be on your model. This will become clearer as we progress through the tutorial. For this tutorial, we will work on creating a human leg with IK set up. I have provided the base human leg model in the file tutorialpart1.mdl. Load this file in Moray now. As you can see from the browser view, all the objects are ungrouped. They are all seperate objects in themselves.


Step 2.
-------

Now that we have our object modeled (in our case the leg), it's time to start thinking about how this object should move. Think about a real human leg for a moment, starting with the foot. A human foot can move around without affecting the rest of the leg. You can swivel it a little bit left and right, a little bit from side to side, and a good bit up and down, all without affecting the lower leg, or the rest of the leg. For this reason, we are going to put our Moray foot on a group on it's own. Create a group called "Foot Group", and put the foot object in it. Now forget about the foor for a moment and think about the lower leg. True, you can move your lower leg around too, but notice how whenever you move your lower leg, the knee is forced to move with it. If you swing your lower leg up and down, the knee rotates with it as well. For this reason, our Moray lower leg and knee will both be put in a new group. Create a group called "Knee and Lower Leg", and put both the knee and lower leg objects in it. Also, seeing as a real foot is (usually) attached to a real lower leg, put our "Foot Group" in our new "Knee and Lower Leg" group. Now think about the upper leg. It is connected to the thigh in the same way as the lower leg is connected to the knee - the two move together. So create a group called "Thigh and Upper Leg", and put the thigh and upper leg objects in it. Also, as a real knee is attached to a real upper leg, put the "Knee and Lower Leg" group in the "Thigh and Upper Leg" group.

You may be pleased to know that that is the most difficult bit out of the way, thinking about how the objects are attached. If you found the above difficult to understand, take a look at the file tutorialpart2.mdl, and see how the groups are arranged. When you are ready, please read on.


Step 3.
-------

The next area we are going to look at are swivel points. These are the points around which the various parts of our model should be allowed to rotate. By default, moray rotates seperate objects around their center point. However, that isn't what we want here. If in the real world the parts of our legs rotated around their centers, then every time you took a step, your lower leg would seperate from your knee and foot! In order for us to work out what points our leg's objects should rotate, think about how real human legs work. Start from the foot and work up. We need to think how the foot can move on it's own, without moving the lower part of the leg it is attached to. With this restriction, we can see that the foot swivels about a point directly below where it meets the lower leg, about in the middle. We need to tell Moray that this is the point we want our Moray foot to rotate about. 

Select our "Foot Group" group (be careful to select the group, and NOT the foot object within it). Now, in the Modify tab underneath the Translate value boxes, you will see a button labled "Local Coordinates". Pressing this button doesn't seem to make an obvious divference, but if you select the translate tool, and drag in the side view, you will see a pair of blue and green lines (the local coordinate axis) move about. These represent the point that our foot group and everything in it will now swivel about. Zoom in on the 2D views until you get a clear view in each of them where the foot meets the lower leg. Now in the side view, translate the local coordinate axis until it is on the horizontal center line of the foot object, and the vertical centre line of the lower leg object. Now check the other 2D views to make sure that the local coordinate axis is lined up to swivel about the correct point. You probably won't have to move it much in the Front or Top views, as I built the origonal leg model around the Z-Axis line. If you were building a model that had objects off the main axises (what is the plural of axis anyway?!), you would have to move the local coordinate axis in all the views.

Now select our "Knee and Lower Leg" group, again being careful not to select just the knee or the lower leg. Again, if you think about how these objects should move, they basically just rotate about the center of the knee. If the Local Coordinates button is still depressed, press it again to turn it off, and then again to turn it back on (a little bug in Moray maybe?). A new set of local coordinates for the knee and lower leg group is created. Translate it to the center of the knee in all views.

Finally select our "Thigh and Upper Leg" group. Again, thinking about a real upper leg and thigh, they both basically rotate about the thigh's center point. If the Local Coordinates button is still depressed, press it again to turn it off, and then again to turn it back on. Move the new local coordinates to the center of the thigh sphere.

Again, if you found the above difficult to understand, take a look at the file tutorialpart3.mdl . The local coordinates are the little coloured lines coming out of the groups at the places I was typing about. When you are ready to move on, please do so.


Step 4.
-------

(In this section, the axises I refer to (X, Y and Z) are Moray's axis definitions)

This is the final step (Yay!). Here we actually set up how the different groups move - basically setting up the IK. Before we begin, think about a real human foot again. Without moving the rest of the leg from the straight position, you can rotate your foot about 5 each way in the Z axis, about 5 each way in the Y axis, and about 10 up and 70 down in the X axis. Select the foot group once more. In the modify tab, inder the Local Coordinates button, you will see a button labled "Freeze". It will change to read "Modify", and you will notice in the scene Browser that a little padlock has appeared in the "Foot Group Box". Also, the Transformation boxes have become grayed out, and you can no longer type in them. Now click on the word Modify. A dialogue box called "Transformation Limits" will pop up. Here we can set how much our groups can be scaled, translated or rotated. We are only interested in rotation for this project. In the "Rotation Components" section, there are tick boxes where you can limit X, Y and Z rotation. The Max and Min boxes let you set the maximum amount and Minimum amount that the curently selected group can be rotated. There is also an IK Usage tick box for each of the X, Y, and Z limits, that will stay grayed out until the Max and Min numbers above it are different.

Lets start with the X-Limit. We have already decided that our foot can rotate up along the X axis by about 70, so set the Min box to "-70" (it's minus because we want the foot to rotate by 70 around Moray's X axis in the negative direction). Now when you select the Max box, the IK Usage tick box becomes available. Set the Max box to "10" (we decided on 10 above). Now place a tick in the IK Usage box. The Dampen field now becomes available. I don't know what this value does, but the default of 360 seems to work fine.

Now lets set the Y-Limit. Follow the same procedure as above, except set the Min limit to "-5" and the max to "5". Do exactly the same for the Z-Limit.

Press OK. The Modify button still says Modify, but the little padlock has dissappeared from the Foot Group in the Browser view.

Think now about how a real human knee can move (we think about the knee and not the lower leg because we set the knee as our swivel point), without disturbing the upper leg and thigh. Depending on which leg you take, it can move about 10 one way and about 30 the other around the Z axis. It can't bend forwards around the X-Axis (unless it's broken, but that can be for another tutorial :) ), but it can bend back by about 150. It can't move ay all along the Y-Axis. Now that we have decided our limits, lets set them up. Select the "Knee and Lower Leg" group, and press freeze, then modify. Now go through the same procedure as setting up the limits for the foot group above, except set the X-Limit Min to "-150", leave the X-Limit Max at "0", leave the Y-Limit alone completely, and set the Z-Min to "-10" and the Z-Max to "30". Click OK.

We're nearly done now. Think about how a thigh can move. It can rotate up on the X axis by about 70, and down by about 20. It can rotate about the Z-Axis about 80 each way. It can also rotate about the Y-Axis away from the body by about 60, and towards the body by about 80 (as long as it doesn't hit the other leg). Lets set up the limits. For this particular step, it does matter if our leg is a left leg or a right leg, so I'm going to take it to be a right leg. Set up the limits in the usual way, taking X-Min and Max to be "-20" and "70", Y-Min and Max to be "-60" and "80", and Z-Min and Max to be "-80" and "80". Press OK.


You're done! 
------------

You have now set up a human leg complete with hopefully realistic IK (my values abobe are only approximate guesses). To actually play around with the leg, select the foot, and use the Inverse Kinematics tool to drag it around in the 2D views. You can change the browser view back into the 3D view now if you want.

Step 3 must be done before Step 4, as once limits are placed, Local coordinates can't be changed unless the limits are removed, so you would be forced to set them up again - not really a major problem, but it could take time. Like I have said before, the above is only an introduction to the basics of IK. I have yet to explore the feature further, but as you can probably see for yourself, it has endless posibilities. All we need now is a way to animate our Moray scene files (someone write an animation plugin PLEASE! :) )

Some instruction on using the Person1.mdl file - I set up the person's hips as the central hub. Place the hips exactly where you want them and then position the rest of the person by using the Inverse Kinematics tool on the two hands, the head or the two feet.

Copywright Information
----------------------
This document is copywright  John Mellerick 1999. I don't really have a problem with anybody using this document or any of the files included with it for their own projects, as long as they give me a credit. Also, for the benefit of everybody out there, I request that if you post any part of any of the documents included in the zip file, that you post the rest of them also. If you want to use any part of this tutorial (including the model files) in an article, magazine, movie, whatever, go ahead - just give me a little credit too, OK?